/*
 * @Author: XiaoFeng Chen 
 * @Date: 2018-11-14 10:33:49 
 * @Last Modified by: XiaoFeng Chen
 * @Last Modified time: 2018-11-14 11:11:16
 * 修复js在运算时存在精度问题
 */

 //采用银行算法，四舍六入五乘双
const _fixed= Number.prototype.toFixed ;

/**
 * 原生的toFixed
 * @param {d} 小数位
 * @returns {String}
 */
export var _toFixed=function(d){
    return _fixed.call(this,d);
}
/**
 * 采用银行算法的toFixed，四舍六入五成双（为5时，检查前面一位是否为偶数）
 * @param {d} 小数位
 * @returns {String}
 */
export var toFixed = function (d) {
    var s = Number(this) + "";
    if (!d) d = 0;
    if (s.indexOf('e') != -1) {
        //暂不处理金额过大的数据，采用默认的Fixed
        return _fixed.call(this,d);
    };
    if (s.indexOf(".") == -1) s += ".";
    s += new Array(d + 1).join("0");

    if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)(\\d*)$").test(s)) {
        var s = "0" + RegExp.$2,
            pm = RegExp.$1,
            a = RegExp.$3.length,
            b = true,
            c = RegExp.$4 * 1,
            e, f;
        if (a == d + 2) {
            a = s.match(/\d/g);
            e = parseInt(a[a.length - 1]);
            f = parseInt(a[a.length - 2]);
            if (e > 4) {
                if (e > 5 || (e === 5 && (c || (f % 2)))) {
                    for (var i = a.length - 2; i >= 0; i--) {
                        a[i] = parseInt(a[i]) + 1;
                        if (a[i] == 10) {
                            a[i] = 0;
                            b = i != 1;
                        } else break;
                    }
                }
            }
            s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2");

        }
        if (b) s = s.substr(1);
        return (pm + s).replace(/\.$/, "");
    }
    return this.valueOf() + "";
};

//除0的结果是0
function calculate(rule, s, n, d) {
    /*
     * d：默认格式化2位的字符串；
     * 	  >=0保留几位小数的数值；
     * 	  -1不格式化的数值
     */
    s = s.toString();
    n = n.toString();
    var sA1 = s.split(".")[1],
        nA1 = n.split(".")[1],
        p1 = sA1 ? sA1.length : 0, //小数位
        p2 = nA1 ? nA1.length : 0,
        p = Math.max(p1, p2),
        rate = Math.pow(10, p),
        zArr = [],
        result; //结果
    s = s.replace(".", ""); //处理成整数
    n = n.replace(".", "");
    if (p != p1) {
        zArr.length = p - p1 + 1;
        s = s + zArr.join("0");
    }
    if (p != p2) {
        zArr.length = p - p2 + 1;
        n = n + zArr.join("0");
    }
    result = eval(Number(s) + rule + "(" + Number(n) + ")"); //计算
    if (rule == "+" || rule == "-" || rule == "%") { //还原
        result = result / rate;
    };
    if (rule == "*") { //还原
        result = result / (rate * rate);
    };
    if (rule == "/" && Number(n) == 0) {
        result = 0;
    }
    //格式化
    if (d=== void 0){
        return result;
    }else{
        result = result.toFixed(d);
        return Number(result);
    }
}

/**
 * 加
 * @param {Number|String} num
 * @param {Number} d 小数位
 * @returns {Number}
 */
export var plus = function (numA,num, d) {
    return calculate("+", numA, num, d);
}
/**
 * 减
 * @param {Number|String} num
 * @param {Number} d 小数位
 * @returns {Number}
 */
export var minus = function (numA,num, d) {
    return calculate("-", numA, num, d);
}
/**
 * 乘
 * @param {Number|String} num
 * @param {Number} d 小数位
 * @returns {Number}
 */
export var mul = function (numA,num, d) {
    return calculate("*",numA, num, d);
}

/**
 * 除
 * @param {Number|String} num
 * @param {Number} d 小数位
 * @returns {Number}
 */
export var div = function (numA,num, d) {
    return calculate("/", numA, num, d);
}

/**
 * 取余
 * @param {Number|String} num
 * @returns {Number}
 */
export var askYu = function (numA,num) {
    return calculate("%",numA, num);
}